﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Windows.Forms;
using WeifenLuo.WinFormsUI.Docking;
using BADDGUI.Controls;
using System.Reflection;

namespace BADDGUI
{
    public partial class frmFmmm : DockContent
    {
        #region Declaration

        ToolStripItem[] tsiMenu;
        TreeNode[] trnNodes; 

        #endregion

        #region Constructor & Load

        public frmFmmm()
        {
            InitializeComponent();
        }

        protected override void OnLoad(EventArgs e)
        {
            InitializeData();
            InitializeUI();
        }

        #endregion

        #region Events

        private void trvNodes_GotFocus(object sender, EventArgs e)
        {
            if (drSelectedNode != null && drSelectedNode["Parent Trx No"] != DBNull.Value && (string)drSelectedNode["View Frm Name"] != "")
            {
                BLCGlobalObject.FrmMain.dockPanel1.Contents[0].DockHandler.GiveUpFocus();
            }
        }

        private void tspModule_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
        {
            if (e.ClickedItem.GetType() == typeof(BLCModToolStrip))
            {
                this.Text = e.ClickedItem.Text;
                foreach (ToolStripItem tspbtnModule in tspModule.Items)
                {
                    if (tspbtnModule.GetType() == typeof(BLCModToolStrip))
                    {
                        ((BLCModToolStrip)tspbtnModule).Checked = false;
                    }
                }
                ((BLCModToolStrip)e.ClickedItem).Checked = true;

                drSelectedMenu = BLCGlobalData.DtMenu.Rows
                                .Cast<DataRow>()
                                .Where(x =>
                                    (int)x["Trx No"] == (int)((BLCModToolStrip)e.ClickedItem).ControlId).Single();

                InitNodes((int)this.SelectedMenu["Trx No"]);
                if (trvNodes.Nodes.Count > 0)
                {
                    trvNodes.SelectedNode = trvNodes.Nodes[0];
                    trvNodes.Focus();
                }
            }
        }

        private void trvNodes_AfterSelect(object sender, TreeViewEventArgs e)
        {
            drSelectedNode = BLCGlobalData.DtNodes.Rows
                             .Cast<DataRow>()
                             .Where(x =>
                                 (int)x["Trx No"] == (int)((BLCTreeNode)e.Node).ControlId).SingleOrDefault();

            if (drSelectedNode != null && drSelectedNode["Parent Trx No"] != DBNull.Value && (string)drSelectedNode["View Frm Name"] != "")
            {
                InitViewForm();
            }
        }

        #endregion

        #region Methods

        public void InitializeData()
        {
        }

        public void InitializeUI()
        {
            BLCGlobalObject.Forms = new List<BLCForm>();
            InitMenu();
            this.trvNodes.GotFocus += trvNodes_GotFocus;

            if (tspModule.Items.Count >= 2)
            {
                tspModule_ItemClicked(null, new ToolStripItemClickedEventArgs(tsiMenu[0]));
            }
        }

        public void InitMenu()
        {
            tsiMenu = new ToolStripItem[BLCGlobalData.DtMenu.Rows.Count];
            int i = 0;
            foreach (DataRow drMenu in BLCGlobalData.dtMenu.Rows)
            {
                tsiMenu[i] = new BLCModToolStrip((int)drMenu["Trx No"], "tsp" + (string)drMenu["Module Code"], (string)drMenu["Module Name"]);
                i++;
            }
            tspModule.Items.AddRange(tsiMenu);
        }

        public void InitNodes(int ModuleTrxNo)
        {
            trvNodes.Nodes.Clear();

            var drParentRows = BLCGlobalData.DtNodes.Rows
                              .Cast<DataRow>()
                              .Where(x =>
                                  x["Parent Trx No"] == DBNull.Value &&
                                  (int)x["Module Trx No"] == ModuleTrxNo).ToList();
            trnNodes = new TreeNode[drParentRows.Count];
            int i = 0;
            foreach (DataRow drNode in drParentRows)
            {
                trnNodes[i] = new BLCTreeNode((int)drNode["Trx No"], "trn" + (string)drNode["Node Name"], (string)drNode["Node Description"]);
                addChildNodes(trnNodes[i], (int)drNode["Trx No"]);
            }

            if (trnNodes.Count() > 0) trvNodes.Nodes.AddRange(trnNodes);
        }

        public void InitViewForm()
        {
            string ProjectName = System.Reflection.Assembly.GetExecutingAssembly().GetName().Name;
            Type typSelectBaseFormName = Type.GetType(((string)SelectedNode["View Base Frm Key"]) + ", " + ProjectName);
            string strSelectFormName = (string)BLCGlobalObject.FrmModuleList.SelectedNode["View Frm Key"];

            BLCForm myForm = BLCGlobalObject.Forms.SingleOrDefault(x => x.ControlName == strSelectFormName);
            if (myForm == null)
            {
                myForm = (BLCForm)Activator.CreateInstance(typSelectBaseFormName);
                BLCGlobalObject.Forms.Add(myForm);
            }
            else if (myForm.IsDisposed) myForm = (BLCForm)Activator.CreateInstance(typSelectBaseFormName);

            BLCGlobalObject.ActiveForm = myForm;
            myForm.DockHandler.DockPanel = BLCGlobalObject.FrmMain.dockPanel1;
            myForm.DockHandler.DockAreas = WeifenLuo.WinFormsUI.Docking.DockAreas.Document;
            myForm.Activate();
        }

        public void InitDetailForm()
        {
            if (BLCGlobalObject.FrmModuleList.SelectedNode["Detail Base Frm Key"] != DBNull.Value)
            {
                string ProjectName = System.Reflection.Assembly.GetEntryAssembly().GetName().Name;
                Type typSelectBaseFormName = Type.GetType(((string)BLCGlobalObject.FrmModuleList.SelectedNode["Detail Base Frm Key"]) + ", " + ProjectName);
                string strSelectFormName = (string)BLCGlobalObject.FrmModuleList.SelectedNode["Detail Frm Key"];

                BLCForm myForm = (BLCForm)Activator.CreateInstance(typSelectBaseFormName);
                BLCGlobalObject.ActiveForm = myForm;
                myForm.DockHandler.DockPanel = BLCGlobalObject.FrmMain.dockPanel1;
                myForm.DockHandler.DockAreas = WeifenLuo.WinFormsUI.Docking.DockAreas.Document;
                myForm.Activate();
            }
        }

        public TreeNode addChildNodes(TreeNode ParentNode, int ParentTrxNo)
        {
            var drChildRow = BLCGlobalData.DtNodes.Rows
                            .Cast<DataRow>()
                            .Where(x =>
                                x["Parent Trx No"] != DBNull.Value &&
                                (int)x["Parent Trx No"] == ParentTrxNo).ToList();
            int i = 0;
            foreach (DataRow drNode in drChildRow)
            {
                TreeNode ChildNode = new BLCTreeNode((int)drNode["Trx No"], "trn" + (string)drNode["Node Name"], (string)drNode["Node Description"]);
                ParentNode.Nodes.Add(ChildNode);

                var drSubChildRow = BLCGlobalData.DtNodes.Rows
                            .Cast<DataRow>()
                            .Where(x =>
                                x["Parent Trx No"] != DBNull.Value &&
                                (int)x["Parent Trx No"] == (int)drNode["Trx No"]).ToList();
                if (drSubChildRow.Count > 0) addChildNodes(ChildNode, (int)drNode["Parent Trx No"]);
                i++;
            }
            return ParentNode;
        }

        #endregion

        #region Property

        DataRow drSelectedMenu;
        public DataRow SelectedMenu
        {
            get { return drSelectedMenu; }
        }

        DataRow drSelectedNode;
        public DataRow SelectedNode
        {
            get { return drSelectedNode; }
        }

        

        #endregion
    }
}